package com.touzi.listeners;

import com.touzi.beans.dbs.fenhong.Fenhong;
import com.touzi.beans.dbs.users.AddKeyByID;
import com.touzi.beans.dbs.users.ChangeKeyong;
import com.touzi.beans.dbs.users.User;
import com.touzi.mapper.FenhongMapper;
import com.touzi.mapper.UserMapper;
import com.touzi.proconfig.ProjectConfig;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.TimerTask;

public class FenhongTimerTask extends TimerTask {
    private static String CLASS_NAME = "com.touzi.listeners.FenhongTimerTask";

    SqlSessionFactory sqlSessionFactory;
    Logger logger;

    public FenhongTimerTask(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
        logger= LoggerFactory.getLogger(this.getClass());
    }

    @Override
    public void run() {
        logger.info("开始分红");

        SqlSession sqlSession = sqlSessionFactory.openSession();
        FenhongMapper fenhongMapper = sqlSession.getMapper(FenhongMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

//        所有需要进行分红的记录
        try {
            List<Fenhong> prepare = fenhongMapper.selectPrepareToFh();
            AddKeyByID add = new AddKeyByID();
            for (Fenhong f : prepare) {
                fenhongMapper.updatIDFenhSucc(f.id);
                add.setId(f.yonghu);
                add.setKeyong(f.jiner);
                userMapper.addKeyongByID(add);
            }
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
    }
}
